package com.lizhenhua.fmmall.controller;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfig;

import com.lizhenhua.fmmall.entity.Orders;
import com.lizhenhua.fmmall.mapper.OrdersMapper;
import com.lizhenhua.fmmall.service.OrderService;
import com.lizhenhua.fmmall.service.timerJob.MyPayConfig;
import com.lizhenhua.fmmall.vo.ResStatus;
import com.lizhenhua.fmmall.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/order")
@Api(value = "订单接口", tags = "订单管理")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation(value = "添加-订单")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "cids", value = "购物车的cid", required = true, defaultValue = "61,84"),
//            @ApiImplicitParam(dataType = "Orders", name = "orders", value = "Orders类", required = true)
    })
    @PostMapping("/add") // 由对象接受添加@RequestBody
    public ResultVo add(String cids, @RequestBody Orders orders, @RequestHeader("token") String token) {
        ResultVo resultVo = null;
        try {
            Map<String, String> orderInfo = orderService.addOrder(cids, orders);
            String orderId = orderInfo.get("orderId");
            String productNames = orderInfo.get("productNames");
            if (orderId != null) {

                // 通过hashMap 设置需要的当前信息
                // 支付订单的参数
                HashMap<String, String> data = new HashMap<>();
                data.put("body", productNames); //购买商品的文字说明  是api文档用的description
                data.put("out_trade_no", orderId);// 使用当前用户订单的编号作为 当前支付的交易的交易号
                data.put("fee_type", "CNY"); // 支付币种 CNY 人民币
//                data.put("total_fee", orders.getActualAmount() * 100 + ""); // 他的单位是 分 9980  代表 99.8 元
                data.put("total_fee", "1");
                data.put("trade_type", "NATIVE");// native  表示交易的类型
                data.put("notify_url", "http://124.222.244.89:8888/pay/callback");  //设置 支付完成时回调方法的接口 (notify:通知)


                // 微信支付 申请支付链接  WXPayConfig是一个接口
                // MyPayConfig 使我们写的一个实现类
                WXPayConfig wxPayConfig = new MyPayConfig();


                // 需要把我们的config 传过来 里面有我们的的商户信息
                WXPay wxPay = new WXPay(wxPayConfig);
                Map<String, String> resp = wxPay.unifiedOrder(data);//微信相应的数据
// 里面的  code_url -> weixin://wxpay/bizpayurl?pr=VVGoqXhzz 这个就是支付链接
                System.out.println(resp);
                // 从微信支付平台获取支付链接
                String codeUrl = resp.get("code_url");
                orderInfo.put("payUrl", codeUrl);

                resultVo = new ResultVo(ResStatus.OK, "提交订单成功", orderInfo);

            } else {
                resultVo = new ResultVo(ResStatus.NO, "提交订单失败", null);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            resultVo = new ResultVo(ResStatus.NO, "提交订单失败", null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultVo;

    }


    @ApiOperation(value = "获取状态码-订单")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "oid", value = "订单id", required = true),
    })
    @GetMapping("/status/{oid}")
    public ResultVo getOrderStatus(@PathVariable("oid") String orderId, @RequestHeader("token") String token) {

        ResultVo resultVo = orderService.getOrderById(orderId);
        return resultVo;
    }


    @ApiOperation(value = "根据条件获取订单信息-订单")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "userId", value = "用户id", required = true,defaultValue = "15"),
            @ApiImplicitParam(dataType = "String", name = "status", value = "订单状态码", defaultValue = "1"),
            @ApiImplicitParam(dataType = "int", name = "pageNum", value = "页码", required = true,defaultValue = "1"),
            @ApiImplicitParam(dataType = "int", name = "limit", value = "每页几条数据", required = true,defaultValue = "5"),
    })
    @GetMapping("/getOrderList")
    public ResultVo getOrderList( String userId,String status,int pageNum,int limit, @RequestHeader("token") String token) {

        ResultVo resultVo = orderService.listOrders(userId, status, pageNum, limit);
        return resultVo;
    }




}
